//-------------------------------------------------------------------------
// Copyright (c) 2000-2010 Ufinity. All Rights Reserved.
//
// This software is the confidential and proprietary information of
// Ufinity
//
// Original author: Administrator
//
//-------------------------------------------------------------------------
// UFINITY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. UFINITY SHALL NOT BE
// LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
// MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
//
// THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
// CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
// PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
// NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
// SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
// SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
// PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). UFINITY
// SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
// HIGH RISK ACTIVITIES.
//-------------------------------------------------------------------------
package com.password.safe.component;

import org.apache.log4j.Logger;

import com.password.safe.constants.Constants;
import com.password.safe.controller.InitController;
import com.password.safe.controller.LoginController;
import com.password.safe.controller.PasswordSafeController;
import com.password.safe.controller.UpdatePasswordController;
import com.password.safe.dto.Messages;
import com.password.safe.util.InitObserver;
import com.password.safe.util.LoginObserver;
import com.password.safe.util.UpdatePasswordObserver;


public class MainFrame extends AbstractMainFrame implements LoginObserver, UpdatePasswordObserver, InitObserver {
	private static final long serialVersionUID = 3330798770436403069L;
	
	private static Logger logger = Logger.getLogger(MainFrame.class);
	private LoginController loginController;
	private PasswordSafeController passwordSafeController;
	private UpdatePasswordController updatePasswordController;
	private InitController initController;
	
	public void loginSuccess(Messages messages) {
		logger.debug("login success, show main frame...");
		this.setVisible(true);
		getEditArea().setText(passwordSafeController.getPasswords());
	}

	public void loginFailed(Messages messages) {
		if (messages.getMessage(Constants.MSG_KEY_LOGOUT) != null) {
			logger.debug("logout request, hidden mainFrame...");
		} else {
			logger.debug("login failed, hidden mainFrame...");
		}
		this.setVisible(false);
	}
	
	public void updatePasswordSuccess(Messages messages) {
		this.setVisible(true);
		handleSaveRequest();
	}

	public void updatePasswordFailed(Messages messages) {
		this.setVisible(false);
	}
	
	public void prepareUpdate() {
		logger.debug("prepare update password, hidden main frame");
		this.setVisible(false);
	}
	
	public void initSuccess(Messages messages) {
		logger.debug("init user success, show main frame...");
		this.setVisible(true);
	}

	public void initFailed(Messages messages) {
		logger.debug("init user failed, hidden main frame...");
		this.setVisible(false);
	}
	
	public void setPasswordSafeController(
			PasswordSafeController passwordSafeController) {
		this.passwordSafeController = passwordSafeController;
		
		// register observer
		this.passwordSafeController.setEditArea(getEditArea());
		this.passwordSafeController.setSaveMenu(getSaveMenu());
	}

	public PasswordSafeController getPasswordSafeController() {
		return passwordSafeController;
	}
	
	public LoginController getLoginController() {
		return loginController;
	}

	public void setLoginController(LoginController loginController) {
		this.loginController = loginController;
		// register loginObserver
		this.loginController.addLoginObserver(this);
	}
	
	public UpdatePasswordController getUpdatePasswordController() {
		return updatePasswordController;
	}

	public void setUpdatePasswordController(
			UpdatePasswordController updatePasswordController) {
		this.updatePasswordController = updatePasswordController;
		this.updatePasswordController.addObserver(this);
	}
	
	public InitController getInitController() {
		return initController;
	}

	public void setInitController(InitController initController) {
		this.initController = initController;
		this.initController.addInitObserver(this);
	}

	@Override
	protected void handleUndoRequest() {
		// TODO Auto-generated method stub
		logger.debug("Click undo button");
		
	}

	@Override
	protected void handleRedoRequest() {
		// TODO Auto-generated method stub
		logger.debug("Click redo button");
		
	}

	@Override
	protected void handleSaveRequest() {
		logger.debug("handle save passwords request...");
		passwordSafeController.savePasswords();
		
	}

	@Override
	protected void handleClearRequest() {
		// TODO Auto-generated method stub
		logger.debug("Click clear button");
		
	}
	
	@Override
	protected void handleUpdatePasswordRequest() {
		logger.debug("user will update password...");
		this.updatePasswordController.prepareUpdate();
	}
	
	@Override
	protected void handleExitRequest() {
		System.exit(EXIT_ON_CLOSE);
	}

	@Override
	protected void handleLogoutRequest() {
		Messages messages = Messages.getInstance();
		messages.addMessage(Constants.MSG_KEY_LOGOUT, "");
		loginController.notifyLoginFailed(messages);
	}

}
